<template>
  <div class="big">
    <div class="count">
      <h3>超级外卖管理系统</h3>
      <el-form
        :model="loginFrom"
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm"
        size="mini"
      >
        <el-form-item prop="account">
          <el-input
            type="text"
            v-model="loginFrom.account"
            autocomplete="off"
            prefix-icon="iconfont icon-yonghu"
          >
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            :type="flag ? 'password' : 'text'"
            v-model="loginFrom.password"
            autocomplete="off"
            prefix-icon="iconfont icon-mima"
          >
            <template #suffix>
              <i
                class="iconfont "
                :class="flag ? 'icon-eyes' : 'icon-eyes-'"
                @click="flag = !flag"
              ></i>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :loading="loginFlag"
            native-type="submit"
            @click="submitForm('ruleForm')"
            >提交</el-button
          >
          <!-- <el-button @click="resetForm('ruleForm')">重置</el-button> -->
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { validateUname, validatePasswad } from '../utils/validator'
import { CalcRouter } from '../router/index.js'
import { login } from '@/api/users.js'
import local from '../utils/local.js'
export default {
  data() {
    return {
      // 数据对象
      loginFrom: {
        account: '',
        password: ''
      },
      // 规则对象
      rules: {
        // validator 校验器    trigger   触发
        account: [{ validator: validateUname, trigger: 'blur' }],
        password: [{ validator: validatePasswad, trigger: 'blur' }]
      },
      flag: true,
      loginFlag: false
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          // 表单验证通过发请求
          const { code, msg, token, role } = await login(this.loginFrom)
          if (code === 0) {
            this.$message.success(msg)
            this.loginFlag = true
            local.set('token', token)
            local.set('role', role)
            CalcRouter()
            setTimeout(() => {
              this.$router.push('/')
            }, 3000)
          } else {
            this.$message.error(msg)
            this.loginFlag = true
            setTimeout(() => {
              this.loginFlag = false
            }, 3000)
          }
        } else {
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.big {
  height: 100%;
  background-color: #2d3a4b;
  display: flex;
  justify-content: center;
  align-items: center;
  .count {
    width: 350px;
    h3 {
      text-align: center;
      color: #fff;
      font-weight: normal;
      letter-spacing: 4px;
      font-size: 16px;
    }
  }
}
.el-form {
  padding-top: 10px;
  .el-button {
    width: 100%;
  }
}
</style>
